import { _decorator, AudioSource, Component, director, EventTouch, Node } from 'cc';
import { GameData } from '../commons/GameData';
import { GameState } from '../commons/GameConst';
import { GameEvent } from '../commons/GameEvent';
const { ccclass, property } = _decorator;

@ccclass('GameReadyView')
export class GameReadyView extends Component {
    

    private _music: AudioSource = null;

    start() {
        this.initUIEvent();

        this._music = this.node.getComponent(AudioSource);
    }

    update(deltaTime: number) {
        
    }


    public show() {
        this.node.active = true;
        if(this._music){
            this._music.play();
        }else{
            this._music = this.node.getComponent(AudioSource);
            this._music.play();
        }
    }

    public hide() {
        this.node.active = false;
        if(this._music){
            this._music.stop();
        }
    }

    private initUIEvent() {
        this.node.on(Node.EventType.TOUCH_END, this.onTouchStart, this);
    }

    private onTouchStart(event: EventTouch) {
        director.emit(GameEvent.GAME_EVENT_START);//开始游戏
        this.hide();
    }

    protected onDestroy(): void {
        this.node.off(Node.EventType.TOUCH_END, this.onTouchStart, this);
    }

}


